#include <iostream>
#include <Trie.hpp>
#include <WordDataReader.hpp>
#include <sstream>
#include <RandomGenerator.hpp>
#include <cstdlib>
#include <ctime>

// araraquara!....!...ve!...

int main (int argc, char * argv[]) {

	srand(time(NULL));

	std::stringstream ss;

	Trie * trie;

	// WordDataReader reader(argv[1]);
	// trie = reader.readData(true);
	// trie->serialize(std::cout);

	///////////////

	trie = new Trie();
	int qtyWords = trie->deserialize(std::cin);

	std::cerr << qtyWords << " words loaded\n";
	int counter = 0;
	// trie->serialize(std::cout);
	std::cout << std::endl;

	for(; counter < qtyWords; ) {


		std::string prefix = RandomGenerator::generate(true);
		std::vector<std::string> retrieve = trie->retrieveUntil(prefix);
		counter += retrieve.size();
		if (retrieve.size()) {
			std::cerr << counter << " / " << qtyWords << std::endl;
			// std::cout << prefix << std::endl;
		}

		for (int i = 0; i < retrieve.size(); i++) {
				std::cout << retrieve[i] << std::endl;
		}

		Trie* subs = trie->detach(prefix);
		if (subs) {
			subs->serialize(ss);
			trie->deserialize(ss);
			delete subs;
		}
	}

	delete trie;
	return 0;
}
